﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Common;
using Microsoft.Practices.CompositeWeb.Utility;
using System.Linq;

namespace ProjectDan.CRM.Views
{
	public partial class EditUploadedFile : Microsoft.Practices.CompositeWeb.Web.UI.Page, IEditUploadedFileView
	{
		private EditUploadedFilePresenter _presenter;

		protected void Page_Load(object sender, EventArgs e)
		{
			if (!this.IsPostBack)
			{
                UploadedFileId = Request.Params["File"].ToNumber();

				this._presenter.OnViewInitialized();
			}
			this._presenter.OnViewLoaded();
		}

		[CreateNew]
		public EditUploadedFilePresenter Presenter
		{
			get
			{
				return this._presenter;
			}
			set
			{
				if(value == null)
					throw new ArgumentNullException("value");

				this._presenter = value;
				this._presenter.View = this;
			}
		}

        public int UploadedFileId
        {
            get
            {
                return ViewState.AsNumber("UploadedFileId");
            }
            set
            {
                ViewState["UploadedFileId"] = value;
            }
        }

        protected UploadedFile instance;

        public void Show(UploadedFile uploadedFile)
        {
            instance = uploadedFile;

            Name = uploadedFile.Name;
            
            Description = uploadedFile.Description;
            
            Tags = uploadedFile.Tags;
            
            FilePath = uploadedFile.FilePath;
        }

        public event EventHandler<DataEventArgs<UploadedFile>> SaveUploadedFile;

        public void OnSaveUploadedFile(UploadedFile uploadedFile)
        {
            var handler = SaveUploadedFile;

            if (handler!=null)
            {
                handler(this, new DataEventArgs<UploadedFile>(uploadedFile));
            }
        }

        public void CompleteSaveUploadedFile()
        {
            Response.Redirect("ListUploadedFiles.aspx");
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);

            _presenter.ReleaseView();
        }

        public string Name { get { return txtName.Text; } set { txtName.Text = value; } }

        public string Description { get { return txtDescription.Text; } set { txtDescription.Text = value; } }

        public string Tags { get { return txtTags.Text; } set { txtTags.Text = value; } }

        public string FilePath { get { return txtFilePath.Text; } set { txtFilePath.Text = value; } }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            instance.Description = Description;

            instance.Name = Name;
            
            instance.Tags = Tags;

            OnSaveUploadedFile(instance);
        }
    }
}

